//===============企业采购目录管理.js


// 客户商品 启禁用
function purchaseEnabled(evt) {

    var selectListId = evt.node.id.split("_")[1];
    var reloadListId = selectListId;

    toCallService({
        formListId: selectListId,
        reloadListId: reloadListId,
        serviceName: "purchaseEnabled",
        msg: "启禁用",
        closeModalId: "",
        param: {},
    });
}

//客户商品 上下架
function purchaseSaled(evt) {

    var selectListId = evt.node.id.split("_")[1];
    var reloadListId = selectListId;

    toCallService({
        formListId: selectListId,
        reloadListId: reloadListId,
        serviceName: "purchaseSaled",
        msg: "上下架",
        closeModalId: "",
        param: {},
    });
}


/**
 * 打开对话框,按钮的id 设置为 xxx_Modalx 如: Buttons_Modal1
 */
function openModal(evt) {
    debugger;
    var modalId = evt.node.id.split("_")[1];
    h3ui.widgets[modalId].props.visible = true;
}

/**
 * 关闭对话框
 */
function closeModal(evt) {
    var modalId = evt.node.id.split("_")[1];
    h3ui.widgets[modalId].props.visible = false;
}

// ============公共方法=======


//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//标签页切换 刷新列表, 要求 标签页id 是 "xxx"+"_"+listid
function tabLoadList(evt) {
    doReloadList({node: {id: evt.data.activeKey.split("_")[1]}});
}


//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}


/**
 * 提交自定义业务服务
 * map:{formListId : "", // select列表id
 * 	reloadListId : "",// 重新加载的list
 * 	serviceName : "",// 服务名称
 * 	msg :"", // 提示内容
 * 	closeModalId:"", // 需要关闭的对话框id, 多个用","隔开
 * 	param : {} // 提交参数 
 * }
 */
function toCallService(map) {

    var selectKeys = h3ui.widgets[map.formListId].props.selectedRowKeys;
    let length = selectKeys.length;
    if (length == 0) {
        h3ui.$message.error("请选择一行数据");
        return false;
    }
    map.param.ids = selectKeys;

    h3ui.$confirm({
        title: map.msg + '确认提交吗',
        onOk() {
            h3ui.services[map.serviceName](map.param).then(res => {
                if (res.success) {
                    h3ui.$message.success('提交成功');

                    if (map.reloadListId) {
                        reloadList(map.reloadListId);
                    }

                    if (map.closeModalId) {
                        map.closeModalId.split(",").forEach(a => {
                            if (a) h3ui.widgets[a].props.visible = false;
                        })
                    }

                } else {
                    h3ui.$message.error(res.errMessage || '提交失败');
                }
            })
        }
    });
}